package com.kin207.netty.tcp;

import java.util.concurrent.TimeUnit;

import com.kin207.netty.msg.decoder.HexMessageDecoder;
import com.kin207.netty.msg.decoder.HexMessageEncoder;
import com.kin207.netty.server.IChannelHandlerInitializer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.timeout.ReadTimeoutHandler;

public class TcpChannelHandlerInitializer implements IChannelHandlerInitializer {
	private int maxFrameLength = 1024*10;

	public void channelHandlerInitializer(ChannelPipeline channelPipeline) {
		 ByteBuf delimiter = Unpooled.copiedBuffer("0x16".getBytes());
		 boolean stripDelimiter = false;

		 channelPipeline.addLast(new ReadTimeoutHandler(12, TimeUnit.SECONDS));
//		 channelPipeline .addLast("decoder", new StringDecoder());
		 channelPipeline.addLast("decoder", new HexMessageDecoder());
		 channelPipeline.addLast("encoder", new HexMessageEncoder());
		 channelPipeline.addLast("framer", new DelimiterBasedFrameDecoder(maxFrameLength,stripDelimiter, delimiter));		 
//		channelPipeline.addLast("framer", new DelimiterBasedFrameDecoder(maxFrameLength, Delimiters.lineDelimiter()));
	}

	public int getMaxFrameLength() {
		return maxFrameLength;
	}

	public void setMaxFrameLength(int maxFrameLength) {
		this.maxFrameLength = maxFrameLength;
	}
	
	

}
